<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sponsors extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
            $data['page']='index';
            $data['sponsors'] = $this->sponsors_model->find_all();
            $this->load->view('template/template',$data);
	}
	
        public function supprimer($id){
            $data['page']='index';
            
            $this->sponsors_model->supprimer($id);
            $data['sponsors'] = $this->sponsors_model->find_all();
            
            $this->load->view('template/template',$data);
        }
        
	public function ajout()
	{		
            $data['page']='ajout';
            if(!empty($_POST)):
                $data['errors'] = array();
                if(empty($_POST["nom"])):
                    $data['errors'][] = "Le nom doit être renseigné !";
                endif;

                if(empty($_POST["description"])):
                    $data['errors'][] = "La description doit être renseigné !";
                endif;

                if((!isset($_FILES['logo']['tmp_name'])||($_FILES['logo']['error'] != UPLOAD_ERR_OK))):
                    $data['errors'][] = "Vous devez spécifier un fichier !";
                endif;

                if(count($data['errors']) == 0):
                    $chemin_destination = '/var/www/assets/img/sponsors/';     
                    var_dump($_FILES['logo']['tmp_name']);
                    var_dump($chemin_destination.$_FILES['logo']['name']);
                    move_uploaded_file($_FILES['logo']['tmp_name'], $chemin_destination.$_FILES['logo']['name']);
                    
                    $this->sponsors_model->ajouter($_POST["nom"], $_POST["description"], $_FILES['logo']['name'], $_POST["link"]);
                    redirect("sponsors");
                endif;
            endif;
            $this->load->view('template/template',$data);
            
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */